set nocompatible              " required
filetype off                  " required

"~/.local/share/nvim/site/autoload/plug.vim
call plug#begin('~/vimfiles/plugged')

"sudo apt install npm
"sudo npm install -g n
"sudo n stable
"npm config set registry https://registry.npm.taobao.org
"sudo npm install -g neovim

Plug 'scrooloose/nerdcommenter'
Plug 'preservim/nerdtree'
Plug 'vim-airline/vim-airline'
Plug 'vim-airline/vim-airline-themes' "airline 的主题
Plug 'ervandew/supertab'
Plug 'honza/vim-snippets'
Plug 'neoclide/coc.nvim'
" Plug 'octol/vim-cpp-enhanced-highlight'
" Plug 'rking/ag.vim'
Plug 'jnurmine/Zenburn'
Plug 'skywind3000/asyncrun.vim'
Plug 'preservim/tagbar'
Plug 'tpope/vim-surround'
" Plug 'dyng/ctrlsf.vim'
Plug 'RRethy/vim-illuminate'
Plug 'airblade/vim-gitgutter'
Plug 'dhruvasagar/vim-table-mode'

call plug#end()            " required

filetype plugin indent on    " required

set laststatus=2
autocmd BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif

" 使用 murphy 调色板
set t_Co=256
"colo torte
colo zenburn

" 设置用于GUI图形用户界面的字体列表。
" set guifont=Courier_New:h14
" set guifont=Ubuntu:h14
" set guifont=Consolas:h14
set guifont=Consolas:h14
"
set nocompatible
" 设定文件浏览器目录为当前目录
set bsdir=buffer
"set autochdir
" 设置编码
set enc=utf-8
" 设置文件编码
set fenc=utf-8
" 设置文件编码检测类型及支持格式
set fencs=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936
" 指定菜单语言
set langmenu=zh_CN.UTF-8
" 设置开启语法高亮
syntax on
" 查找结果高亮度显示
set hlsearch
set cul
set cuc
" tab宽度
set tabstop=4
set expandtab
set cindent shiftwidth=4
set autoindent shiftwidth=4
set number  "显示行号
filetype on
set history=100
set background=dark "设置背景为灰色
"set autoindent "自动对齐，使用上一行的对齐方式
set smartindent "智能对齐方式
set showmatch  "设置匹配模式，类似当输入一个左括号时匹配上相应的那个右括号
set showcmd
set guioptions-=T
set guioptions-=m
set ruler "在编辑过程中，在右下角显示光标位置的状态行
"共享剪切板
set clipboard+=unnamed
"set incsearch "搜索自动定位
set nowrapscan
set nobackup
set noswapfile
set scrolloff=3
hi Normal ctermbg=None
"set lines=35 columns=118
nnoremap <C-J> <C-W><C-J>
nnoremap <C-K> <C-W><C-K>
nnoremap <C-L> <C-W><C-L>
nnoremap <C-H> <C-W><C-H>

nnoremap <expr> gp '`[' . strpart(getregtype(), 0, 1) . '`]'
"set fdm=indent
set foldmethod=indent
set foldlevel=99
set foldlevelstart=1
set foldcolumn=4
nnoremap <space> za

hi illuminatedWord guifg=darkgray
hi illuminatedWord ctermfg=darkgray
" hi illuminatedWord gui=underline guifg=darkgray
" hi illuminatedWord cterm=underline ctermfg=darkgray
" hi illuminatedWord gui=underline
" hi illuminatedWord cterm=underline


" if &term == "xterm"
"     " INSERT mode
"     let &t_SI="\<Esc>[6 q"
"     " REPLACE mode
"     let &t_SR="\<Esc>[3 q"
"     " NORMAL mode
"     let &t_EI="\<Esc>[2 q"
" endif

let mapleader=","

" Shortcuts for the vim-surround
nmap <leader>) ysiw)
nmap <leader>} ysiw}
nmap <leader>" ysiw"
nmap <leader>' ysiw'
nmap <leader>q ysiw'
nmap <leader>Q ysiw"

" Use this option to define the table corner character
let g:table_mode_corner = '|'
" Use this option to define the delimiter which used by
let g:table_mode_delimiter = ' '

" Use `[g` and `]g` to navigate diagnostics
nmap <silent> [g <Plug>(coc-diagnostic-prev)
nmap <silent> ]g <Plug>(coc-diagnostic-next)
" Remap keys for gotos
nmap <silent> gd <Plug>(coc-definition)
nmap <silent> gy <Plug>(coc-type-definition)
nmap <silent> gi <Plug>(coc-implementation)
nmap <silent> gr <Plug>(coc-references)

" popup
nmap <Leader>t <Plug>(coc-translator-p)
vmap <Leader>t <Plug>(coc-translator-pv)
" echo
nmap <Leader>e <Plug>(coc-translator-e)
vmap <Leader>e <Plug>(coc-translator-ev)
" replace
nmap <Leader>r <Plug>(coc-translator-r)
vmap <Leader>r <Plug>(coc-translator-rv)

let g:NERDSpaceDelims = 1
au FileType python let g:NERDSpaceDelims = 0
let g:NERDCompactSexyComs = 1
let g:NERDDefaultAlign = 'left'
let g:NERDAltDelims_java = 1
let g:NERDCustomDelimiters = { 'c': { 'left': '/**','right': '*/' } }
let g:NERDCommentEmptyLines = 1
let g:NERDTrimTrailingWhitespace = 1

" 设置状态栏
let g:airline#extensions#tabline#enabled = 1
let g:airline#extensions#tabline#left_alt_sep = '|'
let g:airline#extensions#tabline#buffer_nr_show = 0
let g:airline#extensions#tabline#formatter = 'default'
let g:airline_theme = 'desertink'  " 主题
let g:airline#extensions#keymap#enabled = 1
let g:airline#extensions#tabline#buffer_idx_mode = 1
let g:airline#extensions#tabline#buffer_idx_format = {
       \ '0': '0 ',
       \ '1': '1 ',
       \ '2': '2 ',
       \ '3': '3 ',
       \ '4': '4 ',
       \ '5': '5 ',
       \ '6': '6 ',
       \ '7': '7 ',
       \ '8': '8 ',
       \ '9': '9 '
       \}
" 设置切换tab的快捷键 <\> + <i> 切换到第i个 tab
nmap <leader>1 <Plug>AirlineSelectTab1
nmap <leader>2 <Plug>AirlineSelectTab2
nmap <leader>3 <Plug>AirlineSelectTab3
nmap <leader>4 <Plug>AirlineSelectTab4
nmap <leader>5 <Plug>AirlineSelectTab5
nmap <leader>6 <Plug>AirlineSelectTab6
nmap <leader>7 <Plug>AirlineSelectTab7
nmap <leader>8 <Plug>AirlineSelectTab8
nmap <leader>9 <Plug>AirlineSelectTab9
" 设置切换tab的快捷键 <\> + <-> 切换到前一个 tab
nmap <leader>- <Plug>AirlineSelectPrevTab
" 设置切换tab的快捷键 <\> + <+> 切换到后一个 tab
nmap <leader>+ <Plug>AirlineSelectNextTab
" 修改了一些个人不喜欢的字符
if !exists('g:airline_symbols')
    let g:airline_symbols = {}
endif
let g:airline_symbols.linenr = "" " current line
let g:airline_symbols.whitespace = "|"
let g:airline_symbols.maxlinenr = "" "maxline
let g:airline_symbols.branch = "BR"
let g:airline_symbols.readonly = "RO"
let g:airline_symbols.dirty = "DT"
let g:airline_symbols.crypt = "CR"

let g:tagbar_ctags_auto=0
let g:tagbar_width = 30
let g:tagbar_right = 1
let g:tagbar_sort = 0
nmap <F2> :TagbarToggle<CR>
set updatetime=300

" 显示行尾空格
highlight ExtraWhitespace ctermbg=red guibg=red
autocmd ColorScheme * highlight ExtraWhitespace ctermbg=red guibg=red
match ExtraWhitespace /\s\+$/

" 删除行尾空格
nmap <F3> :%s/\s\+$//e<CR>
autocmd BufWritePre *.c :%s/\s\+$//e
autocmd BufWritePre *.cpp :%s/\s\+$//e
autocmd BufWritePre *.py :%s/\s\+$//e
autocmd BufWritePre *.sh :%s/\s\+$//e
autocmd BufWritePre *.rs :%s/\s\+$//e

set tags=C:\Users\qiang.cao\ctags
map <F4> :!ctags -R --c++-kinds=+ps --fields=+iaS --extra=+q .<CR>

map <F5> :call RunCurrFile()<CR>
func! RunCurrFile()
    if &filetype == 'python'
        :AsyncRun python %
	elseif &filetype == 'bash'
        :AsyncRun bash %
	elseif &filetype == 'rust'
        :AsyncRun cargo run
	endif
endfunc

let s:fontsize = 14
function! AdjustFontSize(amount)
  let s:fontsize = s:fontsize+a:amount
  :execute "GuiFont! Consolas:h" . s:fontsize
endfunction
" In normal mode, pressing numpad's+ increases the font
noremap <kPlus> :call AdjustFontSize(1)<CR>
noremap <kMinus> :call AdjustFontSize(-1)<CR>
" In insert mode, pressing ctrl + numpad's+ increases the font
inoremap <C-kPlus> <Esc>:call AdjustFontSize(1)<CR>a
inoremap <C-kMinus> <Esc>:call AdjustFontSize(-1)<CR>a

" nmap <F6> <Esc>:!~/vimfiles/python/gitdiff2vim.py %<Enter>:vertical diffsplit %_GITHEAD<Enter><Enter>
nmap <F6> <Esc>:!~/.vim/python/gitdiff2vim.py %<Enter>:vertical diffsplit %_GITHEAD<Enter><Enter>
autocmd BufWinLeave *_GITHEAD silent !rm -rf *_GITHEAD
autocmd BufWinLeave *_GITHEAD silent :diffoff!

nmap <F7> <Esc>:!~/.vim/python/svndiff2vim.py %<Enter>:vertical diffsplit %_SVNHEAD<Enter><Enter>
autocmd BufWinLeave *_SVNHEAD silent !rm -rf *_SVNHEAD
autocmd BufWinLeave *_SVNHEAD silent :diffoff!
autocmd BufWinLeave *_SVNHEAD silent :set nowrap

